package com.study.lock;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.study.entity.other.MzYcResultForm;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 一个简单的死锁类
 * 当DeadLock类的对象flag==1时（td1），先锁定o1,睡眠500毫秒
 * 而td1在睡眠的时候另一个flag==0的对象（td2）线程启动，先锁定o2,睡眠500毫秒
 * td1睡眠结束后需要锁定o2才能继续执行，而此时o2已被td2锁定；
 * td2睡眠结束后需要锁定o1才能继续执行，而此时o1已被td1锁定；
 * td1、td2相互等待，都需要得到对方锁定的资源才能继续执行，从而死锁。
 */
public class DeadLock implements Runnable {
    public int flag = 1;
    //静态对象是类的所有对象共享的
    private static Object o1 = new Object(), o2 = new Object();

    @Override
    public void run() {
        System.out.println("flag=" + flag);
        if (flag == 1) {
            synchronized (o1) {
                try {
                    Thread.sleep(500);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                synchronized (o2) {
                    System.out.println("1");
                }
            }
        }
        if (flag == 0) {
            synchronized (o2) {
                try {
                    Thread.sleep(500);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                synchronized (o1) {
                    System.out.println("0");
                }
            }
        }
    }

    public static void main(String[] args) {
        DeadLock td1 = new DeadLock();
        DeadLock td2 = new DeadLock();
        td1.flag = 1;
        td2.flag = 0;
        //td1,td2都处于可执行状态，但JVM线程调度先执行哪个线程是不确定的。
        //td2的run()可能在td1的run()之前运行
        new Thread(td1).start();
        new Thread(td2).start();
    }

    @Test
    //java截取某个字符之后的字符串
    public void substringTest02(){
        String str = "姚庆福-脱贫不稳定户";
        //获取姓名
        String name = str.substring(0, str.indexOf("-"));
        //根据姓名的起始位置截取后面的预警项
        String warnType = str.substring(name.length()+1);
        System.out.println("截取之后:"+warnType);

        String str1 = "姚庆福-脱贫不稳定户;姚庆福-脱贫不稳定户;";
        String[] split = str1.split(";");
        for (int i=0; i<split.length; i++) {
            System.out.println(split[i]);
        }
    }


    @Test
    public void formTest(){
        /*List<MzYcResultForm> mzYcResultFormList = new ArrayList<>();

        MzYcResultForm mzYcResultForm1 = MzYcResultForm.builder().optChnl("703").matIdetCode("aa").begntime("2023-01-01").endtime("").build();

        mzYcResultFormList.add(mzYcResultForm1);

        System.out.println(JSON.toJSONString(mzYcResultForm1));*/


        String fileName = "黄武_362422199810044312_身份证正面";
        if (StrUtil.isNotEmpty(fileName) && fileName.contains("_")) {
            String[] fileNames = fileName.split("\\_");
            if (fileNames.length > 3) {
                String name = fileNames[0];
                String idCardNo = fileNames[1];
                String fileTypeName = fileNames[2];
                System.out.println(name);
                System.out.println(idCardNo);
                System.out.println(fileTypeName);
            }
        }



        String poorReason = "1,2,3,5";
        String[] poorReasons = poorReason.split("\\,");
        List poorReasonList = new ArrayList();
        Collections.addAll(poorReasonList, poorReasons);
        System.out.println(JSON.toJSONString(poorReasons));
        System.out.println(JSON.toJSONString(poorReasonList));

        System.out.println(DateUtil.now());
    }




}
